<?php

class ArticleController extends Controller {
	public function __construct() {
		if(isset($_COOKIE['is_login']) && $_COOKIE['is_login'] == 1) { } else {
			$this->jump(3,'./index.php?p=back&c=Index&a=login',"请登录");
			die;
		}
	}
	/**
	 *添加文章
	 */
	public function insertAction() {
		if(empty($_POST)) {
			require CURR_VIEW_DIR . "insert.htm";
		} else {
			$title = htmlspecialchars($_POST['title']);
			$content = $_POST['content'];
			$time = time();
			$model = new ArticleModel;
			if($result = $model->insert($title,$content,$time)) {
				$this->jump(3,'./index.php?p=back&c=Article&a=list',"插入成功");
			} else {
				$this->jump(3,'./index.php?p=back&c=Article&a=list',"插入失败");
			}
		}
	}
	/**
	 *管理文章
	 */
	public function listAction() {
		$model = new ArticleModel;
		$rows = $model->select(30);
		$columns = $model->countNum();
		require CURR_VIEW_DIR . "list.htm";
	}
	/**
	 *修改文章
	 */
	public function updateAction() {
		if(empty($_POST)) {
			$id = $_GET['id'];
			$model = new ArticleModel;
			$data = $model->desc($id);
			require CURR_VIEW_DIR . "update.htm";
		} else {
			$id = $_POST['id'];
			$title = $_POST['title'];
			$content = $_POST['content'];
			$model = new ArticleModel;
			if($result = $model->update($title,$content,$id)) {
				$this->jump(3,'./index.php?p=back&c=Article&a=list',"更新成功");
			} else {
				$this->jump(3,'./index.php?p=back&c=Article&a=list',"更新失败");
			}
		}
	}
	/**
	 *删除文章
	 */
	public function deleteAction() {
		if(isset($_GET['id'])) {
			$id = $_GET['id'];	
		} else {
			$this->jump(3,'./index.php?p=home&c=Index&a=index',"非法请求");
		}
		$model = new ArticleModel;
		if($result = $model->del($id)) {
			$this->jump(3,'./index.php?p=back&c=Article&a=list',"删除成功");
		} else {
			$this->jump(3,'./index.php?p=back&c=Article&a=list',"删除失败");
		}
		
	}
}